package com.orm.analysis.action;

import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;

import java.util.List;


public class PaymentAction extends FrontendAction  {

    private GenericService<Payment> service;
    private GenericService<CreditCard> creditCardService;
    private GenericService<Check> checkService;
    private Payment payment;
    private CreditCard creditCard;
    private Check check;
    private List<Payment> payments;
    private String  paymentType;


    public PaymentAction(GenericService<Payment> service  ,GenericService<Check>  checkGenericService,GenericService<CreditCard>  creditCardGenericService) {
        this.service = service;
        this.creditCardService = creditCardGenericService;
        this.checkService = checkGenericService;
    }

    public String paymentTypeSelector() {

        if (paymentType != null ) {
            if(paymentType.equalsIgnoreCase("cash")){
                return "cash";
            } else if(paymentType.equalsIgnoreCase("check")){
                return "check";
            }else if(paymentType.equalsIgnoreCase("credit")){
                return "credit";
            }
        }
        return "success";
    }

    public String getAllPayments() {
        payments = service.list(page, size);
        return SUCCESS;
    }

    public String getBulkPayments() {
                        try {
                            payments = service.findAllWithDepth();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return SUCCESS;
                    }
public String getAllPaymentCustOrder() {
                        try {
                            payments = service.findAllWithDepth("custOrder");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return SUCCESS;
                    }

     public String list() {
         payments = service.list(page, size);
        return SUCCESS;
    }

    public String setUpForInsert() {

        return "success";
    }

    public String setUpForUpdate() {

           if (payment != null && payment.getPaymentId() != null) {
               payment = service.getById(payment.getPaymentId());
           }
           return "success";
       }

    public String insertOrUpdate() {

        if (!validationSuccessful(payment)) {
            return "input";
        } else {
            if (payment.getPaymentId() == null) {
                service.create(payment);
            } else {
                service.update(payment);
            }

        }
        return "success";
    }

    public String cashSetUpForInsert() {


        return "success";
    }
    public String cashSetUpForUpdate() {

        if (payment != null && payment.getPaymentId() != null) {
            payment = service.getById(payment.getPaymentId());
        }
        return "success";
    }


    public String cashInsert() {

        if (!validationSuccessful(payment)) {
            return "input";
        } else {
            if (payment.getPaymentId() == null) {
                service.create(payment);
            }
        }
        return "success";
    }
        public String cashUpdate() {

        if (!validationSuccessful(payment)) {
            return "input";
        } else {

                service.update(payment);


        }
        return "success";
    }

    public String creditCardSetUpForInsert() {


        return "success";
    }
 public String creditCardSetUpForUpdate() {

        if (creditCard != null && creditCard.getPaymentId() != null) {
            creditCard = creditCardService.getById(creditCard.getPaymentId());
        }
        return "success";
    }


    public String creditCardInsert() {

        if (!validationSuccessful(creditCard)) {
            return "input";
        } else {
            if (creditCard.getPaymentId() == null) {
                service.create(creditCard);
            }

        }
        return "success";
    }
public String creditCardUpdate() {

        if (!validationSuccessful(creditCard)) {
            return "input";
        } else {

                service.update(creditCard);


        }
        return "success";
    }

 public String checkSetUpForInsert() {

        return "success";
    }
 public String checkSetUpForUpdate() {

        if (check != null && check.getPaymentId() != null) {
            check = checkService.getById(check.getPaymentId());
        }
        return "success";
    }


    public String checkUpdate() {

        if (!validationSuccessful(check)) {
            return "input";
        } else {

                service.update(check);

        }
        return "success";
    }
public String checkInsert() {

        if (!validationSuccessful(check)) {
            return "input";
        } else {
            if (check.getPaymentId() == null) {
                service.create(check);
            }

        }
        return "success";
    }



    public String cashDelete() {
        service.delete(payment.getPaymentId() );
        return SUCCESS;
    }
    public String creditDelete() {
        service.delete(creditCard.getPaymentId() );
        return SUCCESS;
    }
    public String checkDelete() {
        service.delete(check.getPaymentId() );
        return SUCCESS;
    }


    private boolean validationSuccessful(Payment payment){
        if(payment.getAmount() ==null||payment.getAmount()<0){
       		addActionMessage("Amount is required");
       	}
       	if(payment.getPaymentDate()==null){
       		addActionMessage("PaymentDate is required");
       	}

       	if(this.hasActionMessages()){
       		return false;
       	}else{
       		return true;
       	}
      }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public List<Payment> getPayments() {
        return payments;
    }

    public void setPayments(List<Payment> payments) {
        this.payments = payments;
    }

    public String getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }

    public CreditCard getCreditCard() {
        return creditCard;
    }

    public void setCreditCard(CreditCard creditCard) {
        this.creditCard = creditCard;
    }

    public Check getCheck() {
        return check;
    }

    public void setCheck(Check check) {
        this.check = check;
    }
}
